package com.school.test.search.function.service;

import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.school.test.search.function.assist.IndexAssist;
import com.school.test.search.function.datamodel.TestModel;
import org.bson.Document;

import java.util.Collection;
import java.util.TreeSet;
import java.util.regex.Pattern;

/**
 * @author 马洁
 * @date 2020/1/4 - 12:27
 */
public class TeacherService extends BaseTeaAndStuService{
    public Collection<?> searchName(Pattern regStr) {
        // 符合匹配的教师结果集
        TreeSet<String> resTeacherSet = new TreeSet<>();
        // 将主监考中的老师加入结果集(主监考只有一位)
        IndexAssist.getExam()
                .find(Filters.regex("main_teacher", regStr))
                .iterator()
                .forEachRemaining(doc -> {
                    resTeacherSet.add(doc.getString("main_teacher"));
                });

        // 将副监考的老师加入结果集(副监考有多位), 搜索数组
        searchInExamListByRegStr("deputy_teacher", regStr, resTeacherSet);

        return resTeacherSet;
    }

    public TestModel getTestList(String regTeacherName) {
        // 找到有关该教师的考试搜索结果, 并按照时间戳进行排序
        FindIterable<Document>testSearchList = IndexAssist.getExam()
                .find(Filters.or(
                        Filters.regex("main_teacher", regTeacherName),
                        Filters.regex("deputy_teacher", regTeacherName)))
                .sort(Sorts.ascending("timestamp"));

        return dealTestList(testSearchList);
    }
}
